//
//  LSLeagueHomePageVController.m
//  LookScore
//
//  Created by wr on 2018/1/31.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSLeagueHomePageVController.h"
#import "UIViewController+ZJScrollPageController.h"
#import "LSLeagueHomePageCell.h"
#import "LSLeagueHomePageModel.h"
#import <MJRefresh.h>
@interface LSLeagueHomePageVController ()<UITableViewDelegate,UITableViewDataSource>


@property(strong,nonatomic)UITableView *tableView;
@property (nonatomic, strong) NSMutableArray * dataArray;
//@property(assign,nonatomic)NSInteger  pageIndex;

@end

@implementation LSLeagueHomePageVController
{
    LSLeagueHomePageModel *model;
}

- (NSMutableArray *)dataArray{
    
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    
    return _dataArray;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor =LSBackGroundColor;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
- (void)setUpdate:(NSString *)update{
    _update = update;
    self.season = update;
    [self loadLeagueHomePageData:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
}
-(void)zj_viewDidLoadForIndex:(NSInteger)index{
    

    [self.view addSubview:self.tableView];
    self.tableView.sd_layout.leftEqualToView(self.view).topEqualToView(self.view).rightEqualToView(self.view).bottomEqualToView(self.view);
    
//    model = [LSLeagueHomePageModel mj_objectWithKeyValues:self.homePageData];
//    [self.dataArray addObjectsFromArray:@[model,model,model]];
//    MyLog(@"model ---- %@",model);
    
    LSRefreshHeaderView *header = [LSRefreshHeaderView headerWithRefreshingBlock:^{
        [self loadLeagueHomePageData:YES];
        
    }];
    self.tableView.mj_header = header;// 设置header
    [header beginRefreshing];
    
}
- (void)loadLeagueHomePageData:(BOOL)refresh{
//    if (refresh) {
//
//        self.pageIndex = 1;
//    }else{
//
//        self.pageIndex += 1;
//    }
    
    MyLog(@"leagueId --- %@ /n season --- %@",self.leagueId,self.season);
    [LSHttpEnclosureTool GET:LSLeagueDetail parameters:@{@"leagueId":self.leagueId,@"year":self.season} resBlock:^(id responseObject) {
        
        if (responseObject) {
            if (refresh) {
                [self.dataArray removeAllObjects];
            }
            MyLog(@"leagueHomePage ---- %@",responseObject);
            NSDictionary *dict =[[NSDictionary alloc]initWithObjectsAndKeys:responseObject[@"Rounding"],@"Rounding",responseObject[@"TotalRound"],@"TotalRound",nil];
            [notifiCenter postNotificationName:@"GETROUND" object:nil userInfo:dict];
            model = [LSLeagueHomePageModel mj_objectWithKeyValues:responseObject];
            [self.dataArray addObjectsFromArray:@[model,model,model]];
            MyLog(@"model ---- %@",model);
        }
//        else{
//
//            self.pageIndex -= 1;
//
//        }
        [self endRefreshing];
        [self.tableView reloadData];
    }];
}
- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }
}
#pragma mark 🔥 tableViewDelegate tableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==1) {
        return 220+30;
    }else if (indexPath.row==0){
        return 81;
    }
    return 300;
}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LSLeagueHomePageModel * _model = self.dataArray[indexPath.row];
    if (indexPath.row==0) {
        LSLeagueHomePageCell *cell=[LSLeagueHomePageCell LeagueHomePageCellByTableView:tableView];
        
        cell.model = _model;
        return cell;
    }else if (indexPath.row==1) {
        LSLeagueHomePageRatioCell *cell=[LSLeagueHomePageRatioCell LeagueHomePageRatioCellByTableView:tableView];
        cell.model = _model;
//        KWeakSelf(cell);
//        cell.butonActionBlock = ^(NSInteger tag) {
//            weakcell.selectTag = tag;
////            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//        };
        return cell;
    }else{
        LSLeagueHomePageDataCell *cell=[LSLeagueHomePageDataCell LeagueHomePageDataCellByTableView:tableView];
        cell.model = _model;
        return  cell;
        
    }
    return nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
